package com.sinodata.bsm.cicp.collector.tuxedo;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class TuxedoQueueCollector extends Collector {

    public static Logger logger = Logger.getLogger(TuxedoQueueCollector.class);

    /** 
     * 采集程序实体
     * 
     * @param resAtt
     *            ResAttributeValValue[]
     * @param ip
     *            String
     * @return List
     * @throws CollectorException 
     */

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String cmd = this.paramMap.get("collectCmd");
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, (int) this.interval, 10);
            String line = null;
            StringBuffer sb = new StringBuffer("");
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.matches("(\\S+\\s+){2}\\d+")) {
                    line = line.replaceAll("\\s+", SeparatorConstants.COL_SEPARATOR);
                    sb.append(line);
                    sb.append(SeparatorConstants.LIN_SEPARATOR);
                }
            }
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            propValues[0] = ResPropertyValue.createPropValue(sb.toString(), propMetas[0]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (IOException ioe) {
            logger.error("Collect tuxedo queue error.", ioe);
            throw new CollectorException(ioe);
        }
    }

    /**
     * 构造采集命令
     * 
     * @param resAtt
     *            ResAttributeValValue[]
     * @return String
     */
    @Override
    public String[] getParamMeta() {
        return new String[] { "collectCmd" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "TuxedoQueueList" };
    }

}
